package com.example.消息过期.ttlqueue;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Configuration
public class DelayQueueConfig {

    public static final String DELAY_QUEUE_DELAY_QUE = "ttl_queue";
    public static final String DELAY_EXCHANGE_QUE = "ttl_exchange";

    @Bean
    Queue delayQueueQueue() {
        Map<String, Object> ags=new HashMap<>();
        //给队列设置过期时间
        ags.put("x-message-ttl",10000);
        return new Queue(DELAY_QUEUE_DELAY_QUE, true, false, false,ags);
    }

    /**
     * 定义一个直连交换机
     *
     * @return
     */
    @Bean
    DirectExchange delayQueueExchange() {
        //1. 交换机的名称
        //2。交换机是否持久化
        //3. 如果没有与之绑定的队列，是否删除交换机
        return new DirectExchange(DELAY_EXCHANGE_QUE, true, false);
    }

    /**
     * 这个定义是将交换机和队列绑定起来
     *
     * @return
     */
    @Bean
    Binding deplyQueueBinding() {
        return BindingBuilder
            .bind(delayQueueQueue()).to(delayQueueExchange()).with(DELAY_QUEUE_DELAY_QUE);
    }

}
